package com.chinasofti.user.service.impl;

import java.util.Date;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinasofti.user.dao.mybatis.UserDao;
import com.chinasofti.user.domain.User;
import com.chinasofti.user.service.UserService;
import com.chinasofti.util.GUIDUtils;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	
	@Override
	public User findUserByUserName(User user) {
		User u = null;
		if(user.getUserName()==null||"".equals(user.getUserName())){
			return u;
		}
		u = userDao.findUserByUserName(user.getUserName());
		if(user.getPassword().equals(u.getPassword())){
			return u;
		}
		else{
			return null;
		}
	}

	@Override
	public JSONObject save(User user) {
		JSONObject result = new JSONObject();
		User u =  userDao.findUserByUserName(user.getUserName());
		if(u==null){
			user.setUuid(GUIDUtils.randomGuid());
			user.setCreateTime(new Date());
			userDao.save(user);
			result.put("success", true);
			result.put("obj", user);
			result.put("msg", "注册成功");
		}
		else{
			result.put("success", false);
			result.put("msg", "注册失败,用户名["+user.getUserName()+"]已存在");
		}
		return result;
	}

}